import { menuTree, createMenu, updateMenu, deleteMenu,platformtypeName,detailMenu,menuList } from '@/services/menu';
import { message, Modal } from 'antd';
import {checkList}from '@/utils/utils'

export default {
    namespace: 'permission_management',

    state: {
        modalVisible: false,
        treeList: [],
        platformtnameList:[],
        permission_name: '',    //菜单名称
        permission_id: '',      //菜单id
        route: '',              //菜单路径
        seq: '',                //菜单顺序
        per_id:'',
        updateData: {},
        is_left_menu:'',
        platform_type:'',
        platformtnameSeach:[],
        selectedTab : 'type_route', //选中的tab

    },

    effects: {
        * fetch({ payload }, { call, put }) {
            const response = yield call(menuTree, payload);
            const platformtName = yield call(platformtypeName);
            yield put({
                type: 'save',
                payload: {
                    treeList: response && response.return_msg == 'success' ? response.data : [],
                    platformtnameList:  platformtName && platformtName.return_msg == 'success' ? platformtName.data : [],
                },
            })
            yield put({
              type: 'platSave',
              payload: {
                platformtnameSeach:  platformtName && platformtName.return_msg == 'success' ? platformtName.data : [],
              },
            })
        },
        *  noRouteTypeChange({ payload }, { call, put }){
           const response = yield call(menuTree, payload);
           yield put({
              type: 'save',
              payload: {
                noRouteTreeList: response && response.return_msg == 'success' ? response.data : [],

              },
           })
        },
        * searchTree({ payload }, { call, put }) {
          const response = yield call(menuTree, payload);
          if(response && response.return_code == 'SUCCESS'){
            yield put({
              type: 'save',
              payload: {
                treeList: response.data,
              },
            })
            return true;
          }else{
            return false;
          }
        },
        * searchList({ payload }, { call, put }) {
          const response = yield call(menuList, payload);

          if(response && response.return_code == 'SUCCESS'){
            yield put({
              type: 'save',
              payload: {
                permissionList: response.data,
              },
            })
            return true;
          }else{
            return false;
          }
        },
        * create({ payload }, { call, put }) {
            const data = yield call(createMenu, payload);
            if (data && data.return_code == 'SUCCESS') {
              message.success('新增成功');
              return true;
            }
        },

        * detail({ payload }, { call, put }) {
            const detail = yield call(detailMenu, payload);
            if (detail.return_code === 'SUCCESS') {
              yield put({
                type: 'save',
                payload: {
                  modalType: payload.modalType,
                  modalVisible: true,
                  updateData:detail,
                },
              });
            }
        },
        * showNoRouteModalForUpdate({ payload }, { call, put }) {
          const detail = yield call(detailMenu, payload);
          if (detail.return_code === 'SUCCESS') {
            const response = yield call(menuTree);
            console.log(response);
            yield put({
              type: 'save',
              payload: {
                noRouteModalType: payload.modalType,
                noRouteModalVisible: true,
                noRouteUpdateData:detail,
                noRouteTreeList: response && response.return_msg == 'success' ? response.data : [],
              },
            });
          }
        },
        * update({ payload }, { call, put }) {
            const update = yield call(updateMenu, payload);
            if (update && update.return_code == 'SUCCESS') {
              message.success('修改成功');
			   return true;
            }
        },

        * delete({ payload }, { call, put }) {
            const del = yield call(deleteMenu, payload);
            if (del && del.return_code === 'SUCCESS') {
              message.success('删除成功');
              return true;
            }
        },
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
      platSave(state, action) {
        const platform = action && action.payload && action.payload.platformtnameSeach ? action.payload.platformtnameSeach : [];
        if (state.platformtnameSeach) {
          platform.push({
            k:"0",
            v:"无平台功能",
          })
        }

        return {
          ...state,
          ...action.payload,
          platformtnameSeach:platform,
        };
      },
    },
}
